<!-- eslint-disable no-undef -->
<script setup lang="ts">
import type { ConsultType } from '@/enums'
import ConsultItem from './ConsultItem.vue'
import { getConsultList } from '@/services/consult'
import type { ConsultOrderItem, OrderQuery } from '@/types/consult'
const props = defineProps<{
  type: ConsultType
}>()
const queryParams = ref<OrderQuery>({
  current: 1,
  pageSize: 5,
  type: props.type
})
const list = ref<ConsultOrderItem[]>([])
const loading = ref(false)
const finished = ref(false)
// 获取问诊记录
// 默认会执行一次, 如果数据没有占满屏幕会再次执行onLoad
// 滚动底部时会自动的执行onLoad
// 一次加载结束后需要将loading改成false
const onLoad = async () => {
  const res = await getConsultList(queryParams.value)
  list.value.push(...res.data.rows)
  if (res.data.pageTotal > queryParams.value.current) {
    queryParams.value.current++
  } else {
    finished.value = true
  }
  loading.value = false
}

// 删除订单项
const deleteItem = (id: string) => {
  const i = list.value.findIndex((item) => item.id === id)
  list.value.splice(i, 1)
}
</script>

<template>
  <div class="consult-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <consult-item
        v-for="item in list"
        :key="item.id"
        :item="item"
        @on-delete="deleteItem"
      />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
